package com.softer.chengxin.userinfo.service.impl;

import com.softer.chengxin.common.domin.*;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.common.enums.RoleEnum;
import com.softer.chengxin.common.util.MD5;
import com.softer.chengxin.userinfo.dao.UserInfoDao;
import com.softer.chengxin.userinfo.entity.UserInfo;
import com.softer.chengxin.userinfo.entity.UserSearch;
import com.softer.chengxin.userinfo.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Gong Zhiyang
 * @date 2018/10/13
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoDao userInfoDao;

    /**
     * 查询登录用户的身份
     *
     * @param loginName
     * @return
     */
    @Override
    public Map<String, String> getLoginNameRole(String loginName) {
        return userInfoDao.getLoginNameRole(loginName);
    }

    /**
     * 删除管理用户
     *
     * @param id 用户Id
     * @return 处理结果
     */
    @Override
    public CommonResult deleteUserInfo(String id) {
        int num = userInfoDao.deleteUserInfo(id);
        return CommonResult.response(num);
    }

    /**
     * 新增管理用户
     *
     * @param userInfo  用户信息
     * @param loginName 登陆名
     * @return
     */
    @Override
    public CommonResult addUserInfo(UserInfo userInfo, String loginName) {
        if (StringUtils.isBlank(userInfo.getUsername())) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请添加用户名");
        }
        //检验用户名是否存在
        String userName = userInfoDao.checkUserName(userInfo.getUsername());
        if (StringUtils.isNotBlank(userName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "账号已存在");
        }
        //将密码加密后保存
        String pwd = MD5.EncoderByMd5(userInfo.getPassword());
        userInfo.setPassword(pwd);
        int num = userInfoDao.addUserInfo(userInfo, loginName);
        return CommonResult.response(num);
    }

    /**
     * 获取用户信息
     *
     * @param id 用户id
     * @return 用户信息
     */
    @Override
    public UserInfo getUserInfoById(Integer id) {
        UserInfo userInfo = userInfoDao.getUserInfoById(id);
        if (userInfo != null) {
            return userInfo;
        }
        return new UserInfo();
    }

    /**
     * 编辑用户信息
     *
     * @param userInfo 用户信息
     * @return 处理结果
     */
    @Override
    public CommonResult updateUserInfo(UserInfo userInfo, String loginName) {
        if (StringUtils.isBlank(userInfo.getPassword())) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请添加密码");
        }
        //查询用户ID
        Integer id = userInfo.getId();
        if (id == null) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.OPERATE_FAIL);
        }
        UserInfo userInfoById = userInfoDao.getUserInfoById(id);
        if (userInfoById == null) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "系统繁忙，请稍后重试");
        }
        if (!userInfo.getPassword().equals(userInfoById.getPassword())) {
            //修改过密码，将密码加密后保存
            String pwd = MD5.EncoderByMd5(userInfo.getPassword());
            userInfo.setPassword(pwd);
        }
        int num = userInfoDao.updateUserInfo(userInfo, loginName);
        return CommonResult.response(num);
    }

    /**
     * 检验用户名是否存在
     *
     * @param userName 用户名
     * @return （2：用户名存在，1，用户名不存在）
     */
    @Override
    public int checkUserName(String userName) {
        String un = userInfoDao.checkUserName(userName);
        if (StringUtils.isNotBlank(un)) {
            return NumberEnum.TWO.getNum();
        }
        return NumberEnum.ONE.getNum();
    }

    /**
     * 加载登陆用户创建的用户列表
     *
     * @param page       页面信息
     * @param userSearch 查询实体
     * @param loginName  登陆用户
     * @return 用户列表
     */
    @Override
    public ManagerPageResult load(Page page, String loginName, UserSearch userSearch) {
        ManagerPageResult managerPageResult = new ManagerPageResult();
        JsonResult js = new JsonResult();
        int num = userInfoDao.getUserNum(loginName, userSearch);
        managerPageResult.setCount(num);
        if (num <= Integer.parseInt(Common.ZERO)) {
            return managerPageResult;
        }
        List<UserInfo> load = userInfoDao.load(page, loginName, userSearch);
        for (UserInfo userInfo : load) {
            int rolesid = userInfo.getRolesid();
            if (rolesid == RoleEnum.CITY_MANAGER.getCode() || rolesid == RoleEnum.PROPERTY_MANAGER.getCode()) {
                userInfo.setCorpid("");
                continue;
            }
            if (rolesid == RoleEnum.DISTRICT_MANAGER.getCode()) {
                String corpid = userInfo.getCorpid();
                if (StringUtils.isNotBlank(corpid)) {
                    //查询区域的ID
                    String districtId = corpid.split(",")[0];
                    String districtName = userInfoDao.getDistrictName(Integer.parseInt(districtId));
                    userInfo.setCorpid(districtName);
                }
                continue;
            }
            if (rolesid == RoleEnum.STREET_MANAGER.getCode()) {
                String corpid = userInfo.getCorpid();
                if (StringUtils.isNotBlank(corpid)) {
                    String districtId = corpid.split(",")[0];
                    String districtName = userInfoDao.getDistrictName(Integer.parseInt(districtId));
                    //查询街道的ID
                    String streetId = corpid.split(",")[1];
                    String streetName = userInfoDao.getDistrictName(Integer.parseInt(streetId));
                    StringBuffer name = new StringBuffer(10);
                    name.append(districtName).append("-").append(streetName);
                    userInfo.setCorpid(name.toString());
                }
                continue;
            }
        }
        js.setDs(load);
        managerPageResult.setJson(js);
        return managerPageResult;
    }
}
